package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void save(Permission permission) {
        permissionMapper.insert(permission);

    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listall() {

        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo(permissions);
    }

    @Autowired
    private ApplicationContext ctx;


    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Override
    public void reload() {

        Map<RequestMappingInfo, HandlerMethod> methods1 = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> methods2 = methods1.values();
        for (HandlerMethod handlerMethod : methods2) {
            RequiredPermission annotation = handlerMethod.getMethodAnnotation(RequiredPermission.class);
            System.out.println(annotation);
        }


        /*!!!!获取权限表达式*/
        List<String> expressions = permissionMapper.selectAllexpression();
        //获取每一个控制器对象
        //两种方式
        // 1.直接想要谁就点谁
        //2.控制器是保存在Spring里的所以可以直接用ApplicationContext拿
        //从Spring容器中获取带有@COntroller注解的bean(获取每一个控制器对象)
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //因为输出的是键值对,然后vlues就是拿到它的控制器对象,获取所有的控制器对象
        Collection<Object> controllers = map.values();
        for (Object controller : controllers) {
            Class<?> aClass = controller.getClass();
            //拿到自己的方法,这个DeclaredMethods就是按自己的
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                //获取方法上面我们自己贴的权限注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //判断是否油贴
                if (annotation==null) {
                    continue;//跳过
                }
                //从注解中获取权限名称和权限表达式
                String expremission = annotation.expremission();
                String name = annotation.name();
                //后面按照步骤还得判断这两个在不在数据库中,看笔记的步骤走
                //如果没有就封装成权限对象
                //判断是否存在:如果存在就跳过他不把他封装成对象,如果不存在则封装成对象
                if (expressions.contains(expremission)){
                    continue;
                }

                Permission permission = new Permission();
                permission.setExpression(expremission);
                permission.setName(name);
                //插入到数据库
                permissionMapper.insert(permission);
                //因为我们那个集合是写在循环外面的,无论怎么弄那个集合都是没有数据的所以要对这个集合赋值,然后才继续循环
                expressions.add(expremission) ;
            }
        }
    }

}